﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Warehouse.Web.AppCode;
using System.Data.SqlClient;
using System.Data;
using System.Web.Script.Serialization;

namespace Warehouse.Web.Inventory
{
    public partial class Realtime : BasePage
    {
        protected List<DataAccess.Product> mProducts = null;
        protected List<DataAccess.ProductSpecification> mProductSpecifications = null;
        protected List<DataAccess.Warehouse> mWarehouses = null;
        protected List<DataAccess.InventoryView> mStorage = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        void LoadData()
        {
            mProducts = new List<Warehouse.DataAccess.Product>(DataAccess.Product.GetProducts());
            mProductSpecifications = new List<Warehouse.DataAccess.ProductSpecification>(DataAccess.ProductSpecification.GetProductSpecifications());
            mWarehouses = new List<Warehouse.DataAccess.Warehouse>(DataAccess.Warehouse.GetWarehouses());
            mStorage = new List<Warehouse.DataAccess.InventoryView>(DataAccess.InventoryView.GetList());

            this.rptCategories.DataSource = DataAccess.ProductCategory.GetProductCategories();
            this.rptCategories.DataBind();

            this.rptHouseColumnHeader.DataSource = mWarehouses;
            this.rptHouseColumnHeader.DataBind();
        }

        protected void rptCategories_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item == null)
                return;
            DataAccess.ProductCategory category = e.Item.DataItem as DataAccess.ProductCategory;
            Repeater rptProducts = e.Item.FindControl("rptProducts") as Repeater;
            if (category != null && rptProducts != null)
            {
                List<DataWrapper> list = new List<DataWrapper>();

                foreach (var group in mProducts.Where(p => p.CategoryID == category.ID).GroupBy(arg => arg.SerialNumber).OrderBy(arg => arg.Key))
                {
                    int _ProductSpecificatoinCount = group.Sum(p => Math.Max(1, mProductSpecifications.Count(arg => arg.ProductID == p.ID)));
                    bool firstProduct = true;
                    foreach (var p in group)
                    {
                        DataAccess.ProductSpecification[] _Specifications = mProductSpecifications.Where(arg => arg.ProductID == p.ID).ToArray();
                        int _SpecificatoinCount = _Specifications.Length;
                        bool firstSpecification = true;
                        foreach (var s in _Specifications)
                        {
                            list.Add(new DataWrapper()
                            {
                                FirstProduct = firstProduct,
                                FirstSpecification = firstSpecification,
                                ProductSpecificatoinCount = _ProductSpecificatoinCount,
                                SpecificatoinCount = _SpecificatoinCount,
                                Product = p,
                                Specification = s,
                            });
                            firstSpecification = false;
                            firstProduct = false;
                        }
                        if (firstSpecification)//没有添加任何规格
                        {
                            list.Add(new DataWrapper()
                            {
                                FirstProduct = firstProduct,
                                FirstSpecification = firstSpecification,
                                ProductSpecificatoinCount = _ProductSpecificatoinCount,
                                SpecificatoinCount = _SpecificatoinCount,
                                Product = p,
                                Specification = DataAccess.ProductSpecification.Empty
                            });
                        }
                        firstProduct = false;
                    }
                }

                rptProducts.ItemDataBound += (object obj, RepeaterItemEventArgs arg) =>
                {
                    var rptHouseColumn = arg.Item.FindControl("rptHouseColumn") as Repeater;
                    DataWrapper data = arg.Item.DataItem as DataWrapper;
                    if (rptHouseColumn != null && data != null)
                    {
                        rptHouseColumn.DataSource = mWarehouses.Select(p =>
                        {
                            var storage = mStorage.SingleOrDefault(c => c.HouseID == p.ID && c.ProductSpecificationID == data.Specification.ID);
                            return (storage == null ? 0 : storage.Count);
                        });
                        rptHouseColumn.DataBind();
                    }
                };
                rptProducts.DataSource = list;
                rptProducts.DataBind();
            }
        }


        class DataWrapper
        {
            public bool FirstProduct { get; set; }
            public bool FirstSpecification { get; set; }
            public int ProductSpecificatoinCount { get; set; }
            public int SpecificatoinCount { get; set; }
            public DataAccess.Product Product { get; set; }
            public DataAccess.ProductSpecification Specification { get; set; }
        }
    }
}
